package operation;

import book.*;

import java.util.Scanner;

//查找图书
public class FindOperation implements Operation,IFind{
    @Override
    public int find(String name,BookShelf bookShelf) {
        //仅在有效元素内查找，否则会导致空指针异常
        Book[] books = bookShelf.getBookShelf();
        for(int i = 0; i < bookShelf.getSize(); i++) {
            //如果名字相同，则返回下标
            if(books[i].getTitle().equals(name)) {
                return i;
            }
        }
        return -1;
    }
    @Override
    public void work(BookShelf bookShelf) {
        System.out.println("请输入您要查找的书名：");
        Scanner in = new Scanner(System.in);
        String name = in.nextLine();

        int sub = find(name,bookShelf);
        if(sub==-1) {
            System.out.println("您要查找的书本不存在！");
        } else {
            Book[] books = bookShelf.getBookShelf();
            System.out.println(books[sub]);
        }
    }
}

